package com.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.hospital.bo.EmployeeBo;
import com.hospital.dto.EmployeeExportDto;
import com.hospital.entity.Employee;
import com.hospital.listener.WebEmployeeListener;
import com.hospital.service.intf.EmployeeService;
import com.hospital.util.OssUtil;
import com.hospital.vo.BarVo;
import com.hospital.vo.JovPieVo;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/add.do")
    public R add(Employee employee){
        return employeeService.add(employee);
    }

    @RequestMapping("/delete.do")
    public R delete(Integer id){
        return employeeService.delete(id);
    }

    @RequestMapping("/update.do")
    public R update(Employee employee){
        return employeeService.update(employee);
    }

    @RequestMapping("/deleteByIds.do")
    public R deleteByIds(String ids){
        return employeeService.deleteByIds(ids);
    }

    @RequestMapping("/selectPage.do")
    public PageBean selectPage(String name, String job_name,int page, int limit){
        return employeeService.selectPage(name,job_name, page, limit);
    }

    @RequestMapping("/selectById.do")
    public R selectById(Integer id){
        return employeeService.selectById(id);
    }

    //上传图片
    // springmvc 的 文件上传组件
    @PostMapping("/upload.do")
    public R uploadPic(MultipartFile file) throws IOException {
        //避免预检请求报错  这里有个 if  判断
        if(file!=null){
            //得到文件名
            String originalFilename = file.getOriginalFilename();
            // 将文件转为输入流
            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile(inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok(url);
        }
        return R.fail();
    }

    @RequestMapping("/echartBar.do")
    public R echartBar(){

        List<BarVo> list = employeeService.sexBarData();

        return R.ok(list);
    }

    @RequestMapping("/echartJobPie.do")
    public R echartJobPie(){

        List<JovPieVo> list = employeeService.jobPieData();

        return R.ok(list);
    }

    //导出Excel
    @GetMapping("/export.do")
    public void export(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=employee-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<EmployeeExportDto> list=employeeService.all();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),EmployeeExportDto.class).
                sheet(System.currentTimeMillis()+"").doWrite(list);
    }

    @Autowired
    private WebEmployeeListener webEmployeeListener;

    @RequestMapping("/import.do")
    public R importExcel(MultipartFile file) throws IOException {
        // 避免遇见请求报错
        if(file!=null){
            /**
             * 参数1  文件流
             * 参数2  读取到的数据封装到哪个数据类型中
             * 参数3  监听器  每读取一行数据 就会调用监听器中的 invoke  方法
             * */
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), EmployeeBo.class, webEmployeeListener);
            // 得到 sheet 对象
            ExcelReaderSheetBuilder sheet = read.sheet();
            // 读取sheet
            sheet.doRead();

        }
        return R.ok();
    }

    @RequestMapping("/templateDownload.do")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //读取服务器上的模板文件 写到浏览器
        //得到模板文件的路径
        String realPath = request.getServletContext().getRealPath("/template/员工信息模板.xlsx");

        //根据文件路径 创建File 对象
        File file = new File(realPath);

        // 把File  对象转为 字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("员工信息模板", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();
        // 把字节数组 输出到浏览器
        outputStream.write(bytes);

        outputStream.close();
    }

}
